import BaseRouter from './base-history'
export default class HashRouter extends BaseRouter {
  constructor($router) {
    super($router)
    window.addEventListener('onload', () => {
      this.$router.data.current = this.getPath()
    })
    window.addEventListener('hashchange', () => {
      this.$router.data.current = this.getPath()
    })
  }

  getPath() {
    const hash = window.location.hash
    return hash ? hash.slice(1) : '/'
  }

  push(hash) {
    window.location.hash = hash
    this.$router.data.current = hash;
  }

  replace(hash) {
    const href = window.location.href
    const i = href.indexOf('#')
    const base = i >= 0 ? href.slice(0, i) : href
    const url = `${base}#${hash}`
    window.location.replace(url)
  }

  go(n) {
    window.history.go(n)
  }
}
